//
//  HMStatusCellTopView.swift
//  weibo10
//
//  Created by mac on 15/10/21.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
import SnapKit

class HMStatusCellTopView: UIView {
    var viewModel: HMStatusViewModel?{
        didSet{
            // 昵称
            nameLabel.text = viewModel?.userScreenName
            // 头像
            iconView.sd_setImageWithURL(viewModel?.userProfileImageUrl, placeholderImage: viewModel?.userProfileDefaultImage)
            // 会员
            memberView.image = viewModel?.userMbrank
            // 认证
            vipView.image = viewModel?.userVerifiedType
            // TODO - 来源和创建时间
            sourceLabel.text = "新浪微博"
            timeLabel.text = "就在那时"
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 懒加载控件
    // 头像
    private lazy var iconView: UIImageView = UIImageView(imageName: "avatar_default_big")
    // 昵称
    private lazy var nameLabel: UILabel = UILabel(title: "昵称")
    // 会员
    private lazy var memberView: UIImageView = UIImageView(imageName: "common_icon_membership_level1")
    // 发表时间
    private lazy var timeLabel: UILabel = UILabel(title: "就在那时", titleColor: UIColor.orangeColor(), fontSize: 11)
    // 来源
    private lazy var sourceLabel: UILabel = UILabel(title: "新浪微博", fontSize: 11)
    // 认证图
    private lazy var vipView: UIImageView = UIImageView(imageName: "avatar_vip")
}
// MARK: - 设置界面
extension HMStatusCellTopView{
    private func setupUI(){
        
        let sepView = UIView()
        sepView.backgroundColor = UIColor.lightGrayColor()
        /// 1. 添加控件
        addSubview(sepView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipView)
        /// 2. 自动布局
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.height.equalTo(statusCellMargin)
        }
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(self.snp_left).offset(statusCellMargin)
            make.width.equalTo(statusIconWidth)
            make.height.equalTo(statusIconWidth)
        }
        // 昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(statusCellMargin)
        }
        // 会员
        memberView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(statusCellMargin)
        }
        // 发表时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(statusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel.snp_top)
            make.left.equalTo(timeLabel.snp_right).offset(statusCellMargin)
        }
        // 认证
        vipView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
    }
}